git reflog
Hello everyone. It's Ray.
大家好,我是Ray。
Today is the last day of Ironman. I don't feel like wanting to leave you guys.
今天是鐵人賽的最後一天,真是心有戚戚焉,非常捨不得跟大家分離啊!
Although there are still a plenty of things that I would like to share with you, however, after thinking thoroughly, I've come up with a best choice. How best? It's as powerful as Genki dama(Spirit Bomb) of Goku in DRAGON BALL.
雖然還有很多東西沒有跟大家分享,但在我前思後想,左思右想之後,決定在這最後的一天跟大家分享一個大絕招,有多大呢?就像是孫悟空的元氣彈一樣!
In my previous article I introduced my bro - git rebase
, the greatest magician who is good manipulating history, who is like The Released Exodia in YU-GI-OH.
前幾篇文章我介紹了我的好兄弟-操縱歷史的魔術師, 'git rebase -i',它的威力就像是武藤遊戲的黑暗大法師一樣,非常的作弊啊!
However, in my mind, I still think Genki dama is more powerful. So I decided it as my final item.
不過在我心目中,我還是覺得孫悟空的元氣彈更屌一點,所以就當作是鐵人賽的壓軸啦!
Too much prattling, I am sorry about that.
廢話好像有點太多了,讓我們進入正題。
Have you ever thought of a question? After using git rebase -i
to revise the history, what if we made it wrong?
各位有沒有想過一個問題,在使用了git rebase -i
修改過了歷史之後,萬一修改錯了怎麼辦?
Is there a possibility that no matter what we've done, we could always go back and repeat and repeat and repeat?
有沒有那麼一種可能,不管你做了什麼都可以不用負責任,還可以回到過去讓你做了又做,做了又做呢?
Let me introdue another bro of mine, Doraemon.
讓我來介紹我的另一個好兄弟,小叮噹..
Sorry about that. It's git relog
不是,是git reflog
Vegetable branch told me that she was a bit lonely, so on this meaningful last day, let's give she an chance to make a perfect ending.
vegetable branch跟我說他有點寂寞,好像有一段時間沒他的事了,在這最後的一天,就讓他出來做個完美的ending吧。
git checkout vegetable
git log --oneline
The history on your vegetable branch should look like the image below:
沒意外的話,vegetable branch上的歷史應該會如下圖:
Now I'm going to rebase vegetable branch on top of master branch, and then use git reflog
to go back.
現在我們將把vegetable branch也rebase 到master branch上,然後再使用git reflog
來回朔。
git rebase master
git log --oneline
As photo above, we've successfully rebase vegetable branch on the top of master branch, so what's now?
如上圖,我們已經成功rebase到master branch上,現在該怎麼回到過去呢?
git reflog
You will see a window like photo below:
你應該會看到視窗如下:
Please bear in mind that git reflog
shows the true history. git log
only shows the history of commit, but git reflog
shows all the details of git, even a single git checkout
. It's like a dashcam, which will record whatever it sees and hears.
這邊顯示的是git裡面真正的完整歷史,git log
裡面只會有commit的歷史,但是在git reflog
裡面有所有的歷史,任何的變動哪怕是簡單的一個git checkout
也會在這邊被記錄下來,這就跟我們的行車紀錄器一樣,會將所有看到的,聽到的,都記錄下來。
So if you are doing something you are not supposed to do, bear in mind that git is watching you.
所以啊... 如果各位背著另一半做什麼不應該做的事的時候,不要忘了git正在盯著你啊。
So how could we go back?
那到底該怎麼回到過去呢?
My history is a bit messy becasue it has recorded whatever it should or it shouldn't, so basically my history would look like different from yours.
我個人的歷史有點亂,因為任何該或不該紀錄的git reflog
都會紀錄進去,所以基本上我的歷史應該跟各位的不太一樣。
However, I will show the critical line to you guys, so you could look for it among your histories.
Just copy the checksum of that line as follows:
不過我特別將關鍵的那一行標示出來給大家參考,你們可以在你們的歷史中找到這一行,然後複製該行的驗證碼,如下圖:
Okay. Now we almost have everything.
現在是萬事俱備,只欠東風阿!
git reset c3c2313 --hard
git log --oneline
It's never too late to go back, man!
浪子回頭金不換啊!!
Sounds weird. Okay, now we've gone back to where it was before rebase.
聽起來好像怪怪的,不過沒關係,現在我們又回到rebase 之前啦,如下圖:
Still remember the command git reset --hard
we've shared?
還記得我們之前有分享過git reset --hard
這個指令嗎?
it update the working directory according to the checksum of commit we provided, and remove anything that is incorrespoding to.
他依照我們給的commit驗證碼去將所有的一切更新到符合該commit,並且--hard把不符合該commit的所有東西都從工作資料夾抹去。
Alright, all things come to an end. If you have any questions, feel free to leave your comment below or send me a message. I will do my best to give you an answer within my capability, also learn with you.
好啦,雖然非常地捨不得大家,但是天下無不散的宴席,若大家有git相關的問題歡迎留言或是給我來信,在我能力範圍內我會盡可能的為各位解答,也共同學習。
That's it, happy 2018 Ironman. See you next year.
2018鐵人賽到此告一段落,我們明年見。